﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing.Imaging;
using TeenyGrab.Outputs;
using TeenyGrab.Helpers;
using TeenyGrab.Serialization;

namespace TeenyGrab.Outputs
{
    /// <summary>
    /// Interaction logic for ConfigureFtpOutputWindow.xaml
    /// </summary>
    public partial class ConfigureFtpOutputWindow : Window
    {
        private FtpOutput FtpOutput { get; set; }
        public ConfigureFtpOutputWindow(FtpOutput ftpOutput)
        {
            FtpOutput = ftpOutput;

            InitializeComponent();

            textBoxFtpPath.Text = ftpOutput.FtpUrl;
            textBoxHttpPath.Text = ftpOutput.HttpUrl;
            textBoxUser.Text = ftpOutput.User;
            textBoxPassword.Password = ftpOutput.Pass;

            comboBoxFiletype.ItemsSource = new List<ImageFormat>()
            {
                ImageFormat.Bmp,
                ImageFormat.Emf,
                ImageFormat.Exif,
                ImageFormat.Gif,
                ImageFormat.Icon,
                ImageFormat.Jpeg,
                ImageFormat.Png,
                ImageFormat.Tiff,
                ImageFormat.Wmf,
            };

            if (ftpOutput.Format != null)
            {
                comboBoxFiletype.SelectedValue = ftpOutput.Format.Format;
            }
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            FtpOutput.FtpUrl = textBoxFtpPath.Text;
            FtpOutput.HttpUrl = textBoxHttpPath.Text;
            FtpOutput.User = textBoxUser.Text;
            FtpOutput.Pass = textBoxPassword.Password;
            FtpOutput.Format = new SerializableImageFormat(comboBoxFiletype.SelectedItem as ImageFormat);
            this.Close();
        }

        private void textBoxFtpPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            var caret = textBoxFtpPath.SelectionStart;
            if (!textBoxFtpPath.Text.EndsWith("/"))
                textBoxFtpPath.Text += "/";
            textBoxFtpPath.SelectionStart = caret;
        }

        private void textBoxHttpPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            var caret = textBoxHttpPath.SelectionStart;
            if (!textBoxHttpPath.Text.EndsWith("/"))
                textBoxHttpPath.Text += "/";
            textBoxHttpPath.SelectionStart = caret;
        }
    }
}
